﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using Newtonsoft.Json;
using System.Collections;
using System.IO;

namespace DotNet.WebUI.ADO
{
    /// <summary>
    /// UsersData 的摘要说明
    /// </summary>
    public class UsersData : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");

            //string _LoginID = context.Request["LoginID"].ToString();

            DataSet objDS = getUsers("");
            string _Result = Newtonsoft.Json.JsonConvert.SerializeObject(objDS.Tables[0]);
            context.Response.Write("{\"total\":");

            context.Response.Write(objDS.Tables[0].Rows.Count);
            context.Response.Write(",\"rows\":");
            context.Response.Write(_Result);
            context.Response.Write("}");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public DataSet getUsers(string LoginID)
        {
            string _Sql = @"SELECT * FROM Users 
                WHERE LoginID+Name like '%{0}%' ";
            _Sql = string.Format(_Sql, LoginID);

            SqlConnection objConn = new SqlConnection();
            objConn.ConnectionString = "server=LocalHost;Database=Demo;User ID=soft;password=123456;";
            objConn.Open();

            SqlCommand objCmd = new SqlCommand();
            objCmd.Connection = objConn;
            objCmd.CommandText = _Sql;

            SqlDataAdapter objDA = new SqlDataAdapter();
            objDA.SelectCommand = objCmd;

            DataSet objDS = new DataSet();
            objDA.Fill(objDS);

            return objDS;
        }
    }
}